/*
 * Created by JFormDesigner on Sat Jun 29 21:27:02 CST 2024
 */

package scct.cuixinlong.ui;

import scct.cuixinlong.dao.UserDao;
import scct.cuixinlong.model.User;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;

/**
 * @author Lenovo
 */
public class Formlogin extends JFrame {
    public Formlogin() {
        initComponents();
    }
    private void button1(ActionEvent e) {
        String name=txtName.getText();
        String pass=txtPass.getText();
        System.out.println(name+","+pass);
        if (!name.matches("^\\w{3,10}$")){
            JOptionPane.showMessageDialog(null,"你输入的用户名不合法，请检查后重新输入");
            return;
        }
        if (!pass.matches("^\\w{3,10}$")){
            JOptionPane.showMessageDialog(null,"你输入的密码不合法，请重新输入");
            return;
        }
        UserDao userDao=new UserDao();
        User user=userDao.login(name,pass);
        if (user==null){
            JOptionPane.showMessageDialog(null,"你的账号密码不对，请重新输入。");
        }else{
            JOptionPane.showMessageDialog(null,"登陆成功！！");
        }

        FormMain formMain = new FormMain();
//        formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        formMain.setSize(800,600);

        formMain.setVisible(true);
        formMain.setLocationRelativeTo(null);
        formMain.setVisible(true);
        //隐藏当前窗体
        this.dispose();

    }

//    private void thisPropertyChange(PropertyChangeEvent e) {
//        // TODO add your code here
//    }
//
//    private void btnLogin(ActionEvent e) {
//        // TODO add your code here
//    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - cuixinlong
        label1 = new JLabel();
        txtName = new JTextField();
        btnLogin = new JButton();
        label2 = new JLabel();
        label3 = new JLabel();
        txtPass = new JTextField();
        btnRegister = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label1);
        label1.setBounds(65, 95, 40, 22);
        contentPane.add(txtName);
        txtName.setBounds(110, 85, 185, 34);

        //---- btnLogin ----
        btnLogin.setText("\u786e\u8ba4");
        btnLogin.addActionListener(e -> button1(e));
        contentPane.add(btnLogin);
        btnLogin.setBounds(new Rectangle(new Point(60, 180), btnLogin.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u767b\u5f55");
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 8f));
        contentPane.add(label2);
        label2.setBounds(70, 15, 210, 60);

        //---- label3 ----
        label3.setText("\u5bc6\u7801");
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label3);
        label3.setBounds(65, 125, 40, 25);
        contentPane.add(txtPass);
        txtPass.setBounds(110, 120, 185, 35);

        //---- btnRegister ----
        btnRegister.setText("\u6ce8\u518c");
        contentPane.add(btnRegister);
        btnRegister.setBounds(250, 180, 68, 25);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 305);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - cuixinlong
    private JLabel label1;
    private JTextField txtName;
    private JButton btnLogin;
    private JLabel label2;
    private JLabel label3;
    private JTextField txtPass;
    private JButton btnRegister;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
